from loguru import logger
import os
import sys
from datetime import datetime
import asyncio
from functools import partial
import logging

def setup_exchange_logger():
    """设置交易所初始化日志"""
    # 创建日志目录（如果不存在）
    if not os.path.exists('logs/exchange'):
        os.makedirs('logs/exchange')

    # 配置日志器
    exchange_logger = logger.bind(name="exchange")
    exchange_logger.remove()  # 移除默认处理器
    
    # 添加控制台处理器
    exchange_logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
        level="INFO",
        filter=lambda record: record["extra"].get("name") == "exchange",
        enqueue=True  # 启用异步写入
    )
    
    # 添加文件处理器
    exchange_logger.add(
        f"logs/exchange/exchange-{datetime.now().strftime('%Y%m%d')}.log",
        rotation="1 day",
        retention="7 days",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level="DEBUG",
        filter=lambda record: record["extra"].get("name") == "exchange",
        enqueue=True,  # 启用异步写入
        compression="zip"  # 启用压缩
    )
    
    return exchange_logger

def setup_common_symbols_logger():
    """设置共同交易对日志"""
    # 创建日志目录（如果不存在）
    if not os.path.exists('logs/common_symbols'):
        os.makedirs('logs/common_symbols')

    # 配置日志器
    common_symbols_logger = logger.bind(name="common_symbols")
    common_symbols_logger.remove()  # 移除默认处理器
    
    # 添加控制台处理器
    common_symbols_logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
        level="INFO",
        filter=lambda record: record["extra"].get("name") == "common_symbols",
        enqueue=True
    )
    
    # 添加文件处理器
    common_symbols_logger.add(
        f"logs/common_symbols/common_symbols-{datetime.now().strftime('%Y%m%d')}.log",
        rotation="1 day",
        retention="7 days",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level="DEBUG",
        filter=lambda record: record["extra"].get("name") == "common_symbols",
        enqueue=True,
        compression="zip"
    )
    
    return common_symbols_logger

def setup_arbitrage_logger():
    """配置套利监控器的日志记录器"""
    # 创建日志目录（如果不存在）
    if not os.path.exists('logs/arbitrage'):
        os.makedirs('logs/arbitrage')

     # 配置日志器
    arbitrage_logger = logger.bind(name="arbitrage")
    arbitrage_logger.remove()  # 移除默认处理器
    
    # 添加控制台处理器
    arbitrage_logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
        level="INFO",
        filter=lambda record: record["extra"].get("name") == "arbitrage",
        enqueue=True
    )
    
    # 添加文件处理器
    arbitrage_logger.add(
        f"logs/arbitrage/arbitrage-{datetime.now().strftime('%Y%m%d')}.log",
        rotation="1 day",
        retention="7 days",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level="DEBUG",
        filter=lambda record: record["extra"].get("name") == "arbitrage",
        enqueue=True,
        compression="zip"
    )

    
    return arbitrage_logger

def setup_order_logger():
    """设置订单监控日志"""
    # 创建日志目录（如果不存在）
    if not os.path.exists('logs/order'):
        os.makedirs('logs/order')

    # 配置日志器
    order_logger = logger.bind(name="order")
    order_logger.remove()  # 移除默认处理器
    
    # 添加控制台处理器
    order_logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
        level="INFO",
        filter=lambda record: record["extra"].get("name") == "order"
    )
    
    # 添加文件处理器
    order_logger.add(
        f"logs/order/order-{datetime.now().strftime('%Y%m%d')}.log",
        rotation="1 day",
        retention="7 days",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level="DEBUG",
        filter=lambda record: record["extra"].get("name") == "order"
    )
    
    return order_logger

def setup_logger():
    """设置主日志"""
    if not os.path.exists('logs/main'):
        os.makedirs('logs/main')
        
    main_logger = logger.bind(name="main")
    main_logger.remove()
    
    # 添加控制台处理器
    main_logger.add(
        sys.stdout,
        format="<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
        level="INFO",
        filter=lambda record: record["extra"].get("name") == "main",
        enqueue=True
    )
    
    # 添加文件处理器
    main_logger.add(
        f"logs/main/main-{datetime.now().strftime('%Y%m%d')}.log",
        rotation="1 day",
        retention="7 days",
        format="{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {name}:{function}:{line} - {message}",
        level="DEBUG",
        filter=lambda record: record["extra"].get("name") == "main",
        enqueue=True,
        compression="zip"
    )
    
    return main_logger 